var f = Object.defineProperty;
var d = (o, i, s) =>
  i in o
    ? f(o, i, { enumerable: !0, configurable: !0, writable: !0, value: s })
    : (o[i] = s);
var c = (o, i, s) => (d(o, typeof i != 'symbol' ? i + '' : i, s), s);
import { createNode as m } from '../doc/createNode.js';
import {
  isNode as g,
  isPair as a,
  isCollection as l,
  isScalar as h,
} from './identity.js';
import { NodeBase as p } from './Node.js';
function u(o, i, s) {
  let e = s;
  for (let t = i.length - 1; t >= 0; --t) {
    const n = i[t];
    if (typeof n == 'number' && Number.isInteger(n) && n >= 0) {
      const r = [];
      (r[n] = e), (e = r);
    } else e = /* @__PURE__ */ new Map([[n, e]]);
  }
  return m(e, void 0, {
    aliasDuplicateObjects: !1,
    keepUndefined: !1,
    onAnchor: () => {
      throw new Error('This should not happen, please report a bug.');
    },
    schema: o,
    sourceObjects: /* @__PURE__ */ new Map(),
  });
}
const b = o =>
  o == null || (typeof o == 'object' && !!o[Symbol.iterator]().next().done);
class y extends p {
  constructor(s, e) {
    super(s);
    c(this, 'schema');
    Object.defineProperty(this, 'schema', {
      value: e,
      configurable: !0,
      enumerable: !1,
      writable: !0,
    });
  }
  /**
   * Create a copy of this collection.
   *
   * @param schema - If defined, overwrites the original's schema
   */
  clone(s) {
    const e = Object.create(
      Object.getPrototypeOf(this),
      Object.getOwnPropertyDescriptors(this),
    );
    return (
      s && (e.schema = s),
      (e.items = e.items.map(t => (g(t) || a(t) ? t.clone(s) : t))),
      this.range && (e.range = this.range.slice()),
      e
    );
  }
  /**
   * Adds a value to the collection. For `!!map` and `!!omap` the value must
   * be a Pair instance or a `{ key, value }` object, which may not have a key
   * that already exists in the map.
   */
  addIn(s, e) {
    if (b(s)) this.add(e);
    else {
      const [t, ...n] = s,
        r = this.get(t, !0);
      if (l(r)) r.addIn(n, e);
      else if (r === void 0 && this.schema) this.set(t, u(this.schema, n, e));
      else
        throw new Error(
          `Expected YAML collection at ${t}. Remaining path: ${n}`,
        );
    }
  }
  /**
   * Removes a value from the collection.
   * @returns `true` if the item was found and removed.
   */
  deleteIn(s) {
    const [e, ...t] = s;
    if (t.length === 0) return this.delete(e);
    const n = this.get(e, !0);
    if (l(n)) return n.deleteIn(t);
    throw new Error(`Expected YAML collection at ${e}. Remaining path: ${t}`);
  }
  /**
   * Returns item at `key`, or `undefined` if not found. By default unwraps
   * scalar values from their surrounding node; to disable set `keepScalar` to
   * `true` (collections are always returned intact).
   */
  getIn(s, e) {
    const [t, ...n] = s,
      r = this.get(t, !0);
    return n.length === 0
      ? !e && h(r)
        ? r.value
        : r
      : l(r)
        ? r.getIn(n, e)
        : void 0;
  }
  hasAllNullValues(s) {
    return this.items.every(e => {
      if (!a(e)) return !1;
      const t = e.value;
      return (
        t == null ||
        (s &&
          h(t) &&
          t.value == null &&
          !t.commentBefore &&
          !t.comment &&
          !t.tag)
      );
    });
  }
  /**
   * Checks if the collection includes a value with the key `key`.
   */
  hasIn(s) {
    const [e, ...t] = s;
    if (t.length === 0) return this.has(e);
    const n = this.get(e, !0);
    return l(n) ? n.hasIn(t) : !1;
  }
  /**
   * Sets a value in this collection. For `!!set`, `value` needs to be a
   * boolean to add/remove the item from the set.
   */
  setIn(s, e) {
    const [t, ...n] = s;
    if (n.length === 0) this.set(t, e);
    else {
      const r = this.get(t, !0);
      if (l(r)) r.setIn(n, e);
      else if (r === void 0 && this.schema) this.set(t, u(this.schema, n, e));
      else
        throw new Error(
          `Expected YAML collection at ${t}. Remaining path: ${n}`,
        );
    }
  }
}
c(y, 'maxFlowStringSingleLineLength', 60);
export { y as Collection, u as collectionFromPath, b as isEmptyPath };
